﻿using MvcShopping.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcShopping.Controllers
{
    public class AdminMemberController : Controller
    {
        MvcShoppingContext1 db = new MvcShoppingContext1();
        //
        // GET: /AdminMember/

        public ActionResult Index()
        {
            return View(db.Members.ToList());
        }
        //添加

        //添加用户
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add([Bind(Include = "Email,Password,Name,Nickname,RegisterOn,Address")] Member member)
        {
            var chk_member = db.Members.Where(p => p.Email
                == member.Email).FirstOrDefault();
            if (chk_member != null)
            {
                ModelState.AddModelError("Email",
                    "你输入的Email已经存在");
            }
            if (ModelState.IsValid)
            {
                member.RegisterOn = DateTime.Now;
                member.AuthCode = Guid.NewGuid().ToString();
                db.Members.Add(member);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(member);
        }
        

        //修改用户信息
        public ActionResult Edit(int id)
        {
            Member member = db.Members.Find(id);
            if (member == null)
            {
                return HttpNotFound();
            }
            return View(member);
        }
        

        public ActionResult Edita(String Id,String Password,String Name,String Nickname)
        {
            int id = int.Parse(Id);
              //member.RegisterOn = DateTime.Now;
            //db.Entry(member).State = EntityState.Modified;
            var q = (from t in db.Members where t.Id == id select t).First();
            if (ModelState.IsValid)
            {
                q.Password = Password;
                q.Name = Name;
                q.Nickname = Nickname;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();

        }
        

        //删除用户信息
        public ActionResult Delete(int id)
        {
            Member member = db.Members.Find(id);
            if (member == null)
            {
                return HttpNotFound();
            }
            return View(member);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Member member = db.Members.Find(id);
            db.Members.Remove(member);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
       
        public void Deletea(int id) {
            Member member = db.Members.Find(id);
            db.Members.Remove(member);
            db.SaveChanges();
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

